﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using Timer = System.Windows.Forms.Timer;

public class MainForm : Form
{
    private Button btnShowClock;

    public MainForm()
    {

        this.Text = "透明时钟控制器";
        this.Size = new Size(300, 200);

        btnShowClock = new Button();
        btnShowClock.Text = "显示透明时钟";
        btnShowClock.Size = new Size(150, 50);
        btnShowClock.Location = new Point(75, 50);
        btnShowClock.Click += BtnShowClock_Click;

        this.Controls.Add(btnShowClock);
    }

    private void BtnShowClock_Click(object sender, EventArgs e)
    {
        TransparentClock clock = new TransparentClock();
        clock.Show();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {

    }

    private void MainForm_Load_1(object sender, EventArgs e)
    {

    }
}

public class TransparentClock : Form
{
    [DllImport("user32.dll")]
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll")]
    public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]
    public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);

    const int GWL_EXSTYLE = -20;
    const int WS_EX_LAYERED = 0x80000;
    const int WS_EX_TRANSPARENT = 0x20;
    const int LWA_ALPHA = 0x2;

    private Label timeLabel;

    public TransparentClock()
    {
        this.Text = "透明时钟";
        this.FormBorderStyle = FormBorderStyle.None;
        this.Size = new Size(200, 100);
        this.BackColor = Color.Black;
        this.TopMost = true;

        timeLabel = new Label();
        timeLabel.Font = new Font("Arial", 24, FontStyle.Bold);
        timeLabel.ForeColor = Color.White;
        timeLabel.Dock = DockStyle.Fill;
        timeLabel.TextAlign = ContentAlignment.MiddleCenter;
        this.Controls.Add(timeLabel);

        Timer timer = new Timer();
        timer.Interval = 1000;
        timer.Tick += (s, e) => timeLabel.Text = DateTime.Now.ToString("HH:mm:ss");
        timer.Start();

        // 设置窗口透明
        SetWindowLong(this.Handle, GWL_EXSTYLE,
            GetWindowLong(this.Handle, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT);
        SetLayeredWindowAttributes(this.Handle, 0, 180, LWA_ALPHA);
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.Location = new Point(
            Screen.PrimaryScreen.WorkingArea.Width - this.Width - 20,
            20);
    }
}

// 使用方式：
// Application.Run(new MainForm());